<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://java.sun.com/jsf/core">  

    <ui:composition template="/templates/templateCadastro.xhtml">

        <ui:define name="content">

            <h:form id="form" prependId="false">

                <p:growl showDetail="true" showSummary="true" autoUpdate="true" globalOnly="true" />

                <!-- INICIO CADASTRO -->

                <p:panel id="pnlCad" header="Cadastro de Comarca" >

                    <h:panelGrid id="grid" columns="3" cellspacing="10">

                        <h:outputLabel value="Nome:" style="font-weight: bold"/>

                        <p:inputText id="nome" label="Nome" required="true" requiredMessage="Campo obrigatório" 
                                     title="Nome da Comarca" value="#{comarcaBean.comarca.nome}" size="50" />
                        
                        <p:message for="nome" />

                        <h:outputLabel value="UF" style="font-weight: bold"/>

                        <p:selectOneMenu id="uf" value="#{comarcaBean.uf}" required="true" requiredMessage="Campo obrigatório">
                            <f:selectItem itemLabel="Selecione..." itemValue="" />
                            <f:selectItems value="#{comarcaBean.UFs}" var="uf" itemLabel="#{uf.sigla} - #{uf.estado}" itemValue="#{uf.sigla}" />
                        </p:selectOneMenu>
                        
                        <p:message for="uf" />


                    </h:panelGrid>

                    <div style="text-align: center">

                        <p:commandButton value="Limpar" type="reset" icon="ui-icon-minusthick" />
                        <p:commandButton value="Cadastrar" actionListener="#{comarcaBean.cadastrar()}" rendered="#{comarcaBean.cadastro || comarcaBean.listagem}" icon="ui-icon-disk" update="@form" />
                        <p:commandButton value="Alterar" actionListener="#{comarcaBean.editar()}" rendered="#{comarcaBean.edicao}" icon="ui-icon-pencil" update="@form" />
                        <p:commandButton value="Cancelar" action="#{comarcaBean.cancelar()}" immediate="true" icon="ui-icon-cancel" />

                    </div>

                </p:panel>

                <!-- FIM CADASTRO -->

                <br />

                <!-- INICIO LISTAGEM -->

                <h:panelGroup id="tabela" >

                    <p:dataTable id="basic" var="item" value="#{comarcaBean.comarcas}" emptyMessage="Nenhuma área cadastrada até o momento"
                                 paginator="#{comarcaBean.paginator}" rows="#{comarcaBean.rowNumber}" >

                        <f:facet name="header">
                            Comarcas
                        </f:facet>

                        <p:column headerText="Nome" sortBy="#{item.nome}" style="width: 65%">
                            <h:outputText value="#{item.nome}" />
                        </p:column>
                        
                        <p:column headerText="UF" sortBy="#{item.uf}" style="width: 25%">
                            <h:outputText value="#{item.uf}" />
                        </p:column>

                        <p:column headerText="Ação" style="text-align: center; width: 10%">
                            <h:commandButton id="edit" image="/resources/images/edit.png" actionListener="#{comarcaBean.prepararEditar()}" title="Clique para alterar">
                                <f:setPropertyActionListener value="#{item}" target="#{comarcaBean.comarca}" />
                                <f:ajax execute="@this" render="@form uf" />
                            </h:commandButton>
                            <p:tooltip for="edit" showEffect="slide" hideEffect="slide" />
                            &nbsp;
                            <h:commandButton id="remove" image="/resources/images/remove.png" action="#{comarcaBean.excluir()}" immediate="true" title="Clique para excluir">
                                <f:setPropertyActionListener value="#{item}" target="#{comarcaBean.comarca}" />
                            </h:commandButton>
                            <p:tooltip for="remove" showEffect="slide" hideEffect="slide" />
                        </p:column>

                    </p:dataTable>

                </h:panelGroup>

                <!-- FIM LISTAGEM -->

            </h:form>

        </ui:define>

    </ui:composition>

</html>